package com.go8.admin.goods.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.go8.admin.goods.vo.CatalogTreeVO;
import com.go8.admin.goods.vo.CatalogVO;
import com.go8.common.service.ServiceResponse;
import com.go8.goods.pojo.Catalog;
import com.go8.goods.service.CatalogService;

/**
 * go8-admin后台系统接口
 * @author cjl
 */
@RestController("adminCatalogController")
@RequestMapping("/admin")
public class CatalogController {
	@Autowired
	private CatalogService catalogService;
	
	@PostMapping("catalogs")
	public Object add(@RequestBody Catalog catalog) {
		try {
			catalogService.add(catalog);
			return ServiceResponse.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	@PutMapping("catalogs/{id}")
	public Object update(@RequestBody Catalog catalog) {
		try {
			catalogService.update(catalog);
			return ServiceResponse.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	@DeleteMapping("catalogs/{id}")
	public Object delete(@PathVariable Long id) {
		try {
			catalogService.delete(id);
			return ServiceResponse.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	@GetMapping("catalogs/code")
	public Object getCodeByCode(String code) {
		// 参数检查
		if(StringUtils.isEmpty(code)) {
			return ServiceResponse.illegalArgs();
		}

		try {
			code = catalogService.getCode(code);
			return ServiceResponse.ok(code);
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	@GetMapping("catalogs/{id}")
	public Object getCatalogById(@PathVariable Long id) {
		if(id==null || id.longValue()<=0) {
			return ServiceResponse.illegalArgs();
		}
		try {
			Catalog catalog = catalogService.getCatalogById(id);
			return ServiceResponse.ok(toCatalogVO(catalog));
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	
	@GetMapping("catalogs/sons")
	public Object getCatalogByPid(Long pid) {
		//入口参数必检查，限流，拦截无效请求，安全性，减轻数据库的压力
		if(pid==null || pid.longValue()<0) {
			return ServiceResponse.illegalArgs();
		}

		//执行业务代码
		try {
			List<Catalog> list = catalogService.getSonsByPid(pid);
			List<CatalogTreeVO> vos = new ArrayList<>();
			for(Catalog cat : list) {
				CatalogTreeVO vo = toCatalogTreeVO(cat);
				vos.add(vo);
			}
			return ServiceResponse.ok(vos);
		} catch (Exception e) {
			e.printStackTrace();
			return ServiceResponse.error();
		}
	}
	
	//前端Tree组件强耦合的接口数据转换，不推荐这种设计
	//	//这里只是让大家多写点后台代码，同时提供一种思路
	//	//如果是前后端彻底分离，这里一定不要这样设计
	private CatalogTreeVO toCatalogTreeVO(Catalog cat) {
		CatalogTreeVO vo = new CatalogTreeVO();
		vo.setId(cat.getId());
		vo.setPid(cat.getPid());
		vo.setTitle(cat.getName());
		vo.setParent(cat.getParent());
		return vo;
	}
	
	//通用数据格式对象
	private CatalogVO toCatalogVO(Catalog cat) {
		CatalogVO vo = new CatalogVO();
		vo.setId(cat.getId());
		vo.setPid(cat.getPid());
		vo.setName(cat.getName());
		vo.setCode(cat.getCode());
		vo.setParent(cat.getParent());
		vo.setOrder1(cat.getOrder1());
		return vo;
	}
}
